<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Front;

class News extends Front {

	protected $beforeActionList = array(
		'setModel' => array('except' => 'category'),
	);

	public function lists()
	{
		$id = $this->request->param('id');
		if (!$id) {
			return $this->error("非法操作");
		}
		$cateTree = getParentCategory($id);
		$cate = $this->getCategory($id);
		$category = get_category_child($id);
		$map      = array(
			'category_id' => array('IN', $category),
		);

		$order = "id desc";
		$list  = model('News')->where($map)->order($order)->paginate(12, false,
		    [
		        'type'=>'MyPage'
		    ]);
		
		// 联系方式
		$contact = M('contact')->where(['id'=>1])->find();
		$data = array(
		    'cateTree' => $cateTree,
			'list' => $list,
			'cate' => $cate,
			'page' => $list->render(),
		    'contact' => $contact,
		);
		if ($cate['template_lists']) {
			$teamplate = 'content/' . $cate['template_lists'];
		} else {
			$teamplate = 'content/list';
		}
		$this->setSeo($cate['title'], $cate['keywords'], $cate['description']);
		$this->assign($data);
		return $this->fetch($teamplate);
	}

	public function detail($id = '', $name = '')
	{
	    header("Content-type: text/html; charset=utf-8"); 
	    // 获取内容
	    $info = $this->model->find($id);
		if (empty($info)) {
			return $this->error("无此内容！");
		}
		// 获取子栏目
		$cCate = getChildCategory(21);
		// 获取父栏目
		$pCate = getParentCategory($info['category_id']);
		// 获取当前分类
		$cate = $this->getCategory($info['category_id']);
		// 获取顶级分类
		$topCate = $this->getCategory(21);
		// 相关新闻
		$category = get_category_child($info['category_id']);
		$map      = array(
		    'category_id' => array('IN', $category),
		    'id' => array('NOT IN', $id),
		    'cover_id' => array('NEQ',''),
		);
		$order = "id desc";
		$list  = model('News')->where($map)->order($order)->paginate(6);
		// 联系方式
		$contact = M('contact')->where(['id'=>1])->find();
		
		$data = array(
		    'cCate'=> $cCate,
		    'pCate'=> $pCate,
			'info' => $info,
		    'cate' => $cate,
		    'topCate' => $topCate,
		    'contact' => $contact,
		    'list' => $list,
		);
		if (isset($info['template_detail']) && $info['template_detail']) {
			$teamplate = 'content/' . $this->modelInfo['name'] . '/' . $info['template_detail'];
		} else {
			$teamplate = 'content/' . $this->modelInfo['name'] . '/detail';
		}
		$this->assign($data);
		$title       = isset($info['title']) ? $info['title'] : '';
		$tags        = isset($info['tags']) ? $info['tags'] : '';
		$description = isset($info['description']) ? $info['description'] : '';
		$this->setSeo($title, $tags, $description);
		return $this->fetch($teamplate);
	}
	
	protected function getCategory($id) {
		$data = db('Category')->find($id);
		return $data;
	}

	protected function setModel() {
		$model_name = $this->request->param('model');
		$model_id   = $this->request->param('model_id');
		$row        = db('Model')->select();
		foreach ($row as $key => $value) {
			$name_list[$value['name']] = $value;
			$id_list[$value['id']]     = $value;
		}

		if (empty($name_list[$model_name]) && empty($id_list[$model_id])) {
			return $this->error("无此模型！");
		} else {
			$this->modelInfo = $model_name ? $name_list[$model_name] : $id_list[$model_id];
			$this->model = M($this->modelInfo['name']);

			$this->assign('model_id', $this->modelInfo['id']);
			$this->assign('model_list', $name_list);
		}
	}
}